using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace gpcx
{
    public partial class TopFrm : Form
    {
        [DllImport("user32.dll")]
        private static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        private static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam); 
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;
        public Action AppExit;
        public Action RefreshData;

        public TopFrm()
        {
            InitializeComponent();
            toolStripMenuItem3.Click += toolStripMenuItem3_Click;
            toolStripMenuItem4.Click += toolStripMenuItem3_Click;
            toolStripMenuItem5.Click += toolStripMenuItem3_Click;
            toolStripMenuItem6.Click += toolStripMenuItem3_Click;
            toolStripMenuItem7.Click += toolStripMenuItem3_Click;
            toolStripMenuItem8.Click += toolStripMenuItem3_Click;
            toolStripMenuItem9.Click += toolStripMenuItem3_Click;
            toolStripMenuItem10.Click += toolStripMenuItem3_Click;
            toolStripMenuItem11.Click += toolStripMenuItem3_Click;
            toolStripMenuItem12.Click += toolStripMenuItem3_Click;

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            try
            {
                var loca = ConfigManager.GetAppSettingsKeyValue("TopFrmLocation").Split(',');
                Location = new Point(int.Parse(loca[0]), int.Parse(loca[1]));
            }
            catch
            {
                ConfigManager.UpdateSetting("TopFrmLocation", $"0,0");
            }

            try
            {
                var opacity = ConfigManager.GetAppSettingsKeyValue("TopFrmOpacity");
                Opacity = double.Parse(opacity);
            }
            catch
            {
                ConfigManager.UpdateSetting("TopFrmOpacity", $"{Opacity}");
            }
        }

        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;
            Hide();
        }

        private void toolStripMenuItem3_Click(object sender, System.EventArgs e)
        {
            Opacity = double.Parse((sender as ToolStripMenuItem)?.Text.Replace("%", "")) / 100;
            ConfigManager.UpdateSetting("TopFrmOpacity", $"{Opacity}");
        }

        private void toolStripMenuItem1_Click(object sender, System.EventArgs e)
        {
            Hide();
        }

        private void TopFrm_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            ConfigManager.UpdateSetting("TopFrmLocation", $"{Location.X},{Location.Y}");            
        }

        public void UpdateData(List<ValueTuple<string, string, string, string, string, string, string>> param)
        {
            this.Invoke((EventHandler)delegate
            {
                if (listBox1.Items.Count > 0 && Visible == false)
                    return;

                while (listBox1.Items.Count > param.Count)
                {
                    listBox1.Items.RemoveAt(param.Count);
                }
                for (var i = 0; i < param.Count; i++)
                {
                    if (i >= listBox1.Items.Count)
                    {
                        listBox1.Items.Add($"{param[i].Item1} {param[i].Item5}%");
                    }
                    else
                    {
                        listBox1.Items[i] = $"{param[i].Item1} {param[i].Item5}%";
                    }
                }
                Height = (listBox1.ItemHeight+ 1) * listBox1.Items.Count;
            });
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
            AppExit?.Invoke();
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RefreshData?.Invoke();
        }
    }
}
